/**
 * Created by myself on 2016/7/1.
 */




//删除
    function deleteObj(objid)
    {

        bootbox= bootbox_global;
        // alert(roleid);

        bootbox_global.dialog({
            message: "确定要删除吗?",
            buttons: {
                Cancel: {
                    label: "<i class='icon-info'></i> 取消",
                    "className": "btn-sm btn-danger",
                    callback: function () {
                    }
                }
                , OK: {
                    label: "<i class='icon-ok'></i> 确定",
                    "className": "btn-sm btn-success",
                    callback: function () {

                        var  param = {

                            brands_id:objid,
                            material_id:$("#typeselect2").val(),
                        };


                        //发送请求
                        send_request_direct("/mallBrands/deleteBrands", param, function process_data(data) {


                            //操作成功
                            AlertSuccess(bootbox, "操作成功", function () {

                                grid.Init();


                            });


                        }, function process_fail(data) {

                            //操作失败
                            AlertError(bootbox, data.msg);


                        });

                    }
                }
            }
        });

    }


var bootbox_global;
var grid;
define([
    "jquery",
    "IO",
    "bootbox"
], function ($, IO, bootbox) {

    bootbox_global = bootbox;
    $(function () {

        var columns_select ={name_dataIndex :'name',value_dataIndex :'type_id'};
        var  grid_select = new Select_Grid("/mall/getMaterialTypeList",$("#typeselect"),columns_select,"rows=100000");

        grid_select.removeflag = 1;
        grid_select.Init(function(){

            dataInit();
            var columns_select2 ={name_dataIndex :'name',value_dataIndex :'material_id'};
            var  grid_select2 = new Select_Grid("/mall/getMaterialListPaging",$("#typeselect2"),columns_select2,"rows=100000&type_id="+$("#typeselect").val());

            grid_select2.removeflag = 1;
            grid_select2.Init(function(){

                dataInit();

            });

        });
        //监听一级菜单相应二级菜单
        $("#typeselect").change(function(){
            var columns_select2 ={name_dataIndex :'name',value_dataIndex :'material_id'};
            var  grid_select2 = new Select_Grid("/mall/getMaterialListPaging",$("#typeselect2"),columns_select2,"rows=100000&type_id="+$("#typeselect").val());


            grid_select2.removeflag = 1;
            grid_select2.Init(function(){

                dataInit();

            });
        });

        //品牌下拉框
        var columns_select3 ={name_dataIndex :'name',value_dataIndex :'brands_id'};
        var  grid_select3 = new Select_Grid("/mallBrands/getListPaging",$("#typeselect3"),columns_select3,"rows=100000");


        grid_select3.removeflag = 1;
        grid_select3.Init(function(){

            dataInit();

        });

        //每列数据列性
        var columns = [

            {
                title: '编号', dataIndex: 'User_ID', dataSource: function (data, datarow, gridobj, current_column) {

                return "<span>" + parseInt((gridobj.current_page - 1) * gridobj.page_rows + parseInt(datarow) + 1) + "</span>";
            }
            },
            {title: '名称', dataIndex: 'name',},
            {
                title: '操作', dataIndex: 'User_ID', dataSource: function (data, datarow, gridobj, current_column) {

                var html = "<div class='div_btn'><button onclick=\"deleteObj(" + data[datarow].brands_id + ")\"  class='btn btn-danger'>删除</button>";


                html = html + "</div>"

                return html;
            }
            },


        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/mallBrands/getBrandsListPaging", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var param = "material_id=" + $("#typeselect2").val();

            return param;

        }

        //dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });


        //新增
        $("#btn_add").bind("click", function () {


            var alertarray = {
                form: {api: "/mallBrands/addBrands", name: "alertform", id: "alertform"},
                inputs: [

                    {title: "品牌", type: "select", id: "roles", form_name: "brands_id", copyid: "typeselect3"},

                ]

            }
            bootbox.dialog({
                title: "新增",
                message: generateAlertForm(alertarray),
                buttons: {
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-danger",
                        "callback": function () {
                        }
                    },
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-success",
                        "callback": function () {

                            //验证
                            if (check_form_info_all(alertarray.form.name) == false)
                                return false;

                            //发送请求
                            ajax_request(alertarray.form.name, "&material_id="+$("#typeselect2").val()+"&type_id="+$("#typeselect").val(), function process_data(data) {


                                //操作成功
                                AlertSuccess(bootbox, "操作成功", function () {

                                    grid.Init();


                                });


                            }, function process_fail(data) {

                                //操作失败
                                AlertError(bootbox, data.msg);


                            });


                        }
                    }
                }
            });


        });
    });


});
